﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.User
{
    public partial class AddUser : BasePage
    {
        private UserFilter userFilter = new UserFilter();
        private ExcuteDao userManage = new ExcuteDao();

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                //设置需要添加的数据
                SetData();
                //添加用户信息到数据库中
                if (userManage.Insert_User(userFilter) == -1)
                {
                    isOk = true;
                    foreach (CheckBoxList cbl in cblRole.Controls)
                    {
                        foreach (ListItem ckRole in cbl.Items)
                        {
                            if (ckRole.Selected)
                            {
                                if (userManage.Insert_UserRole(userFilter.User_ID, ckRole.Value) > -1)
                                {
                                    isOk = false;
                                }
                            }
                        }
                    }
                }

                if (isOk)
                {
                    ReturnList("UserList.aspx", "添加用户成功！");
                    // Response.Write("<script>top.$('#loading').show();location.replace('UserList.aspx?msg=添加用户成功！');</script>");
                }
                else
                {
                    ShowMsg("添加用户失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }

        }
        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtUserId.Text.TrimEnd()))
            {
                ShowMsg("请输入用户ID！", 1);
                txtUserId.Focus();
                return false;
            }

            //if (string.IsNullOrEmpty(txtPassword.Text.TrimEnd()))
            //{
            //    ShowMsg("请输入密码！", 1);
            //    txtPassword.Focus();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(txtPasswordConfirm.Text.TrimEnd()))
            //{
            //    ShowMsg("请输入确认密码！", 1);
            //    txtPasswordConfirm.Focus();
            //    return false;
            //}

            if (!txtPassword.Text.Equals(txtPasswordConfirm.Text.TrimEnd()))
            {
                ShowMsg("密码两次输入的不一样！", 1);
                txtPasswordConfirm.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtUserName.Text.TrimEnd()))
            {
                ShowMsg("请输入真实姓名！", 1);
                txtUserName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtEmail.Text.TrimEnd()))
            {
                ShowMsg("请输入Email！", 1);
                txtEmail.Focus();
                return false;
            }
            #region 用户角色选择判断
            bool ischk = false;
            foreach (CheckBoxList cbl in cblRole.Controls)
            {
                foreach (ListItem ckRole in cbl.Items)
                {
                    if (ckRole.Selected)
                    {
                        ischk = true;
                    }
                }
            }

            if (!ischk)
            {
                ShowMsg("请选择用户角色！", 1);
                return false;
            }
            #endregion

            return checkUser();


        }

        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <returns></returns>
        private bool checkUser()
        {
            DataTable dtcheck = userManage.Get_UserByID(txtUserId.Text.TrimEnd().ToUpper());

            if (dtcheck != null && dtcheck.Rows.Count > 0)
            {
                ShowMsg("用户已经存在请确认！", 1);
                txtUserId.Focus();
                return false;
            }
            return true;
        }
        /// <summary>
        /// 设置页面数据到Filter
        /// </summary>
        private void SetData()
        {
            userFilter.User_ID = SpecialCharHandler.HtmlEncode(txtUserId.Text.TrimEnd());
            userFilter.User_Name = SpecialCharHandler.HtmlEncode(txtUserName.Text.TrimEnd());

            if (string.IsNullOrEmpty(txtPassword.Text.TrimEnd()))
            {
                userFilter.User_pwd = AppFunction.Encrypt(SpecialCharHandler.HtmlEncode(txtUserId.Text.TrimEnd()));
            }
            else
            {
                userFilter.User_pwd = AppFunction.Encrypt(SpecialCharHandler.HtmlEncode(txtPassword.Text.TrimEnd()));
            }
            userFilter.User_Tel = SpecialCharHandler.HtmlEncode(txtTel.Text.TrimEnd());
            userFilter.User_Mail = SpecialCharHandler.HtmlEncode(txtEmail.Text.TrimEnd());
            userFilter.User_status = ddlStatus.SelectedValue;
        }
    }
}